package viewPackage.menus;

import inputPackage.KeyResource;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import mainPackage.GFrame;

@SuppressWarnings("serial")
public class KeyboardMenu extends JPanel {

	GFrame gameFrame;
	
	private MenuLabel r2dKey;
	private MenuLabel l2dKey;
	private MenuLabel r3dKey;
	private MenuLabel l3dKey;
	private MenuLabel u3dKey;
	private MenuLabel d3dKey;
	private MenuLabel jumpKey;
	private MenuLabel switchKey;
	private MenuLabel pauseKey; 
	
	private KeyResource k;
	
	public KeyboardMenu(GFrame frame) {
		super();
		this.gameFrame = frame;
		
		this.setBackground(Color.BLACK);
		
		k = gameFrame.getKeyResource();
		
		//These are the static labels.  They'll just be yellow labels. No clicking or anything.
		MenuLabel title = new MenuLabel("Keyboard Configuration", 48);
		MenuLabel twoD = new MenuLabel("2D", 20);
		MenuLabel threeD = new MenuLabel("3D", 20);
		MenuLabel both = new MenuLabel("Both", 20);
		MenuLabel right2D = new MenuLabel("Right", 20);
		MenuLabel left2D = new MenuLabel("Left", 20);
		MenuLabel right3D = new MenuLabel("Right", 20);
		MenuLabel left3D = new MenuLabel("Left", 20);
		MenuLabel up3D = new MenuLabel("Up", 20);
		MenuLabel down3D = new MenuLabel("Down", 20);
		MenuLabel jump = new MenuLabel("Jump", 20);
		MenuLabel switchView = new MenuLabel("Switch Views", 20);
		MenuLabel pause = new MenuLabel("Pause", 20);
		
		//These are simple dynamic buttons.  They'll have simple mouse-over and click stuff.
		MenuLabel restore = new MenuLabel("Restore Defaults", 20);
		MenuLabel save = new MenuLabel("Save", 20);
		MenuLabel cancel = new MenuLabel("Cancel", 20);
		
		//This is the tricky stuff.  These labels will both be buttons and dynamically updating text.
		//These will need a specific extra method to update their labels.
		r2dKey = new MenuLabel(KeyEvent.getKeyText(k.getRight2D()), 20);
		l2dKey = new MenuLabel(KeyEvent.getKeyText(k.getLeft2D()), 20);
		r3dKey = new MenuLabel(KeyEvent.getKeyText(k.getRight3D()), 20);
		l3dKey = new MenuLabel(KeyEvent.getKeyText(k.getLeft3D()), 20);
		u3dKey = new MenuLabel(KeyEvent.getKeyText(k.getUp3D()), 20);
		d3dKey = new MenuLabel(KeyEvent.getKeyText(k.getDown3D()), 20);
		jumpKey = new MenuLabel(KeyEvent.getKeyText(k.getJump()), 20);
		switchKey = new MenuLabel(KeyEvent.getKeyText(k.getSwitchViews()), 20);
		pauseKey = new MenuLabel(KeyEvent.getKeyText(k.getPause()), 20); 
		
		//Give them some labels.
		r2dKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
		l2dKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
		r3dKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
		l3dKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
		u3dKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
		d3dKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
		jumpKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
		switchKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
		pauseKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
		
		//These are standard dynamic labels.  They don't need much more than a mouse listener.
		restore.addMouseListener(new JLabelMouse2(restore, this) {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				k.restoreDefaults();
				km.updateKeyLabels();
			}});
		
		save.addMouseListener(new JLabelMouse(save) {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				k.writeToFile();
				gameFrame.getContentPane().removeAll();
				gameFrame.getContentPane().add(new OptionsPanel(gameFrame), BorderLayout.CENTER);
				gameFrame.validate();
				gameFrame.repaint();				
			}});
		
		cancel.addMouseListener(new JLabelMouse(cancel) {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				gameFrame.getContentPane().removeAll();
				gameFrame.getContentPane().add(new OptionsPanel(gameFrame), BorderLayout.CENTER);
				gameFrame.validate();
				gameFrame.repaint();
			}});
		
		
				
		//These are the screwy mouse listener and key listener ones.
		r2dKey.addMouseListener(new JLabelMouse2(r2dKey, this) {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				r2dKey.setBorder(BorderFactory.createLineBorder(Color.RED, 3));
				super.mouseEntered(arg0);
			}		
			@Override
			public void mouseExited(MouseEvent arg0) {
				r2dKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
				super.mouseExited(arg0);
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				r2dKey.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
				r2dKey.setForeground(Color.CYAN);
				r2dKey.removeMouseListener(this);
				r2dKey.addKeyListener(new SetKeyListener(this) {				
					public void keyPressed(KeyEvent e) {
						k.setRight2D(e.getKeyCode());
						this.getJlm().km.updateKeyLabels();
						this.getJlm().getLabel().addMouseListener(this.getJlm());
						this.getJlm().getLabel().setForeground(Color.YELLOW);
						this.getJlm().getLabel().setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
						this.getJlm().getLabel().removeKeyListener(this);
					}});
				r2dKey.grabFocus();
			}});
		
		
		l2dKey.addMouseListener(new JLabelMouse2(l2dKey, this) {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				l2dKey.setBorder(BorderFactory.createLineBorder(Color.RED, 3));
				super.mouseEntered(arg0);
			}		
			@Override
			public void mouseExited(MouseEvent arg0) {
				l2dKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
				super.mouseExited(arg0);
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				l2dKey.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
				l2dKey.setForeground(Color.CYAN);
				l2dKey.removeMouseListener(this);
				l2dKey.addKeyListener(new SetKeyListener(this) {				
					public void keyPressed(KeyEvent e) {
						k.setLeft2D(e.getKeyCode());
						this.getJlm().km.updateKeyLabels();
						this.getJlm().getLabel().addMouseListener(this.getJlm());
						this.getJlm().getLabel().setForeground(Color.YELLOW);
						this.getJlm().getLabel().setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
						this.getJlm().getLabel().removeKeyListener(this);
					}});
				l2dKey.grabFocus();
			}});
		
		
		r3dKey.addMouseListener(new JLabelMouse2(r3dKey, this) {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				r3dKey.setBorder(BorderFactory.createLineBorder(Color.RED, 3));
				super.mouseEntered(arg0);
			}		
			@Override
			public void mouseExited(MouseEvent arg0) {
				r3dKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
				super.mouseExited(arg0);
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				r3dKey.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
				r3dKey.setForeground(Color.CYAN);
				r3dKey.removeMouseListener(this);
				r3dKey.addKeyListener(new SetKeyListener(this) {				
					public void keyPressed(KeyEvent e) {
						k.setRight3D(e.getKeyCode());
						this.getJlm().km.updateKeyLabels();
						this.getJlm().getLabel().addMouseListener(this.getJlm());
						this.getJlm().getLabel().setForeground(Color.YELLOW);
						this.getJlm().getLabel().setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
						this.getJlm().getLabel().removeKeyListener(this);
					}});
				r3dKey.grabFocus();
			}});
		
		
		l3dKey.addMouseListener(new JLabelMouse2(l3dKey, this) {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				l3dKey.setBorder(BorderFactory.createLineBorder(Color.RED, 3));
				super.mouseEntered(arg0);
			}		
			@Override
			public void mouseExited(MouseEvent arg0) {
				l3dKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
				super.mouseExited(arg0);
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				l3dKey.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
				l3dKey.setForeground(Color.CYAN);
				l3dKey.removeMouseListener(this);
				l3dKey.addKeyListener(new SetKeyListener(this) {				
					public void keyPressed(KeyEvent e) {
						k.setLeft3D(e.getKeyCode());
						this.getJlm().km.updateKeyLabels();
						this.getJlm().getLabel().addMouseListener(this.getJlm());
						this.getJlm().getLabel().setForeground(Color.YELLOW);
						this.getJlm().getLabel().setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
						this.getJlm().getLabel().removeKeyListener(this);
					}});
				l3dKey.grabFocus();
			}});
		
		
		u3dKey.addMouseListener(new JLabelMouse2(u3dKey, this) {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				u3dKey.setBorder(BorderFactory.createLineBorder(Color.RED, 3));
				super.mouseEntered(arg0);
			}		
			@Override
			public void mouseExited(MouseEvent arg0) {
				u3dKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
				super.mouseExited(arg0);
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				u3dKey.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
				u3dKey.setForeground(Color.CYAN);
				u3dKey.removeMouseListener(this);
				u3dKey.addKeyListener(new SetKeyListener(this) {				
					public void keyPressed(KeyEvent e) {
						k.setUp3D(e.getKeyCode());
						this.getJlm().km.updateKeyLabels();
						this.getJlm().getLabel().addMouseListener(this.getJlm());
						this.getJlm().getLabel().setForeground(Color.YELLOW);
						this.getJlm().getLabel().setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
						this.getJlm().getLabel().removeKeyListener(this);
					}});
				u3dKey.grabFocus();
			}});
		
		
		d3dKey.addMouseListener(new JLabelMouse2(d3dKey, this) {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				d3dKey.setBorder(BorderFactory.createLineBorder(Color.RED, 3));
				super.mouseEntered(arg0);
			}		
			@Override
			public void mouseExited(MouseEvent arg0) {
				d3dKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
				super.mouseExited(arg0);
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				d3dKey.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
				d3dKey.setForeground(Color.CYAN);
				d3dKey.removeMouseListener(this);
				d3dKey.addKeyListener(new SetKeyListener(this) {				
					public void keyPressed(KeyEvent e) {
						k.setDown3D(e.getKeyCode());
						this.getJlm().km.updateKeyLabels();
						this.getJlm().getLabel().addMouseListener(this.getJlm());
						this.getJlm().getLabel().setForeground(Color.YELLOW);
						this.getJlm().getLabel().setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
						this.getJlm().getLabel().removeKeyListener(this);
					}});
				d3dKey.grabFocus();
			}});
		
		
		jumpKey.addMouseListener(new JLabelMouse2(jumpKey, this) {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				jumpKey.setBorder(BorderFactory.createLineBorder(Color.RED, 3));
				super.mouseEntered(arg0);
			}		
			@Override
			public void mouseExited(MouseEvent arg0) {
				jumpKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
				super.mouseExited(arg0);
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				jumpKey.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
				jumpKey.setForeground(Color.CYAN);
				jumpKey.removeMouseListener(this);
				jumpKey.addKeyListener(new SetKeyListener(this) {				
					public void keyPressed(KeyEvent e) {
						k.setJump(e.getKeyCode());
						this.getJlm().km.updateKeyLabels();
						this.getJlm().getLabel().addMouseListener(this.getJlm());
						this.getJlm().getLabel().setForeground(Color.YELLOW);
						this.getJlm().getLabel().setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
						this.getJlm().getLabel().removeKeyListener(this);
					}});
				jumpKey.grabFocus();
			}});
		
		
		switchKey.addMouseListener(new JLabelMouse2(switchKey, this) {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				switchKey.setBorder(BorderFactory.createLineBorder(Color.RED, 3));
				super.mouseEntered(arg0);
			}		
			@Override
			public void mouseExited(MouseEvent arg0) {
				switchKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
				super.mouseExited(arg0);
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				switchKey.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
				switchKey.setForeground(Color.CYAN);
				switchKey.removeMouseListener(this);
				switchKey.addKeyListener(new SetKeyListener(this) {				
					public void keyPressed(KeyEvent e) {
						k.setSwitchViews(e.getKeyCode());
						this.getJlm().km.updateKeyLabels();
						this.getJlm().getLabel().addMouseListener(this.getJlm());
						this.getJlm().getLabel().setForeground(Color.YELLOW);
						this.getJlm().getLabel().setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
						this.getJlm().getLabel().removeKeyListener(this);
					}});
				switchKey.grabFocus();
			}});
		
		
		pauseKey.addMouseListener(new JLabelMouse2(pauseKey, this) {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				pauseKey.setBorder(BorderFactory.createLineBorder(Color.RED, 3));
				super.mouseEntered(arg0);
			}		
			@Override
			public void mouseExited(MouseEvent arg0) {
				pauseKey.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
				super.mouseExited(arg0);
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				pauseKey.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
				pauseKey.setForeground(Color.CYAN);
				pauseKey.removeMouseListener(this);
				pauseKey.addKeyListener(new SetKeyListener(this) {				
					public void keyPressed(KeyEvent e) {
						k.setPause(e.getKeyCode());
						this.getJlm().km.updateKeyLabels();
						this.getJlm().getLabel().addMouseListener(this.getJlm());
						this.getJlm().getLabel().setForeground(Color.YELLOW);
						this.getJlm().getLabel().setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
						this.getJlm().getLabel().removeKeyListener(this);
					}});
				pauseKey.grabFocus();
			}});
		
		
	
		//Grid bag constraints.
		Constraints Ctitle = new Constraints(0,0,4,1,true);
		Constraints C2D = new Constraints(0,1,2,1,false);
		Constraints C3D = new Constraints(2,1,2,1,false);
		Constraints CBoth = new Constraints(0,4,2,1,false);
		Constraints Cr2D = new Constraints(0,2,1,1,false);
		Constraints Cl2D = new Constraints(0,3,1,1,false);
		Constraints Cr3D = new Constraints(2,2,1,1,false);
		Constraints Cl3D = new Constraints(2,3,1,1,false);
		Constraints Cu3D = new Constraints(2,4,1,1,false);
		Constraints Cd3D = new Constraints(2,5,1,1,false);
		Constraints Cjump = new Constraints(0,5,1,1,false);
		Constraints Cswitch = new Constraints(0,6,1,1,false);
		Constraints Cpause = new Constraints(0,7,1,1,false);
		Constraints Csave = new Constraints(1,9,1,1,false);
		Constraints Ccancel = new Constraints(2,9,1,1,false);
		Constraints Cdefault = new Constraints(1,8,2,1,true);
		
		Constraints Cr2DK = new Constraints(1,2,1,1,false);
		Constraints Cl2DK = new Constraints(1,3,1,1,false);
		Constraints Cr3DK = new Constraints(3,2,1,1,false);
		Constraints Cl3DK = new Constraints(3,3,1,1,false);
		Constraints Cu3DK = new Constraints(3,4,1,1,false);
		Constraints Cd3DK = new Constraints(3,5,1,1,false);
		Constraints CjumpK = new Constraints(1,5,1,1,false);
		Constraints CswitchK = new Constraints(1,6,1,1,false);
		Constraints CpauseK = new Constraints(1,7,1,1,false);
		
		this.setLayout(new GridBagLayout());
		
		this.add(title, Ctitle);
		this.add(twoD, C2D);
		this.add(threeD, C3D);
		this.add(both, CBoth);
		this.add(right2D, Cr2D);
		this.add(left2D, Cl2D);
		this.add(right3D, Cr3D);
		this.add(left3D, Cl3D);
		this.add(up3D, Cu3D);
		this.add(down3D, Cd3D);
		this.add(jump, Cjump);
		this.add(switchView, Cswitch);
		this.add(pause, Cpause);
		this.add(save, Csave);
		this.add(cancel, Ccancel);
		this.add(restore, Cdefault);
		this.add(r2dKey, Cr2DK);
		this.add(l2dKey, Cl2DK);
		this.add(r3dKey, Cr3DK);
		this.add(l3dKey, Cl3DK);
		this.add(u3dKey, Cu3DK);
		this.add(d3dKey, Cd3DK);
		this.add(jumpKey, CjumpK);
		this.add(switchKey, CswitchK);
		this.add(pauseKey, CpauseK);
		
	}
	
	private void updateKeyLabels() {
		r2dKey.setText(KeyEvent.getKeyText(k.getRight2D()));
		r2dKey.repaint();
		l2dKey.setText(KeyEvent.getKeyText(k.getLeft2D()));
		l2dKey.repaint();
		r3dKey.setText(KeyEvent.getKeyText(k.getRight3D()));
		r3dKey.repaint();
		l3dKey.setText(KeyEvent.getKeyText(k.getLeft3D()));
		l3dKey.repaint();
		u3dKey.setText(KeyEvent.getKeyText(k.getUp3D()));
		u3dKey.repaint();
		d3dKey.setText(KeyEvent.getKeyText(k.getDown3D()));
		d3dKey.repaint();
		jumpKey.setText(KeyEvent.getKeyText(k.getJump()));
		jumpKey.repaint();
		switchKey.setText(KeyEvent.getKeyText(k.getSwitchViews()));
		switchKey.repaint();
		pauseKey.setText(KeyEvent.getKeyText(k.getPause()));
		pauseKey.repaint();
	}	
}